﻿using System;
using System.Linq;
using Microsoft.Linq.Expressions;
using Microsoft.Scripting.Actions;
using Cjc.ExpressionEngine.Excel.Runtime.Types;
using Microsoft.Scripting.Actions.Calls;
using System.Reflection;

namespace Cjc.ExpressionEngine.Excel.Runtime.Binding
{
	public class ConversionBinder : ConvertAction
	{
		public ExcelBinder Binder { get; private set; }
		public Expression Context { get; private set; }
		public ConversionResultKind ResultKind { get; private set; }

		public ConversionBinder( ExcelBinder binder, Expression context, Type type, ConversionResultKind resultKind )
			: base( type, resultKind == ConversionResultKind.ExplicitCast || resultKind == ConversionResultKind.ExplicitTry )
		{
			this.Binder = binder;
			this.Context = context;
			this.ResultKind = ResultKind;
		}

		public override MetaObject Fallback( MetaObject self, MetaObject onBindingError )
		{
			return Binder.ConvertTo( ToType, ResultKind, self );
		}

		public override object HashCookie
		{
			get { return this; }
		}
	}
}